<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'imageverification:userAuthentication:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #facePhoto="{ record }">
        <Image v-if="record.facePhoto" :src="record.facePhoto" width="100" height="100px" alt="" />
        <div style="height: 100px;text-align: center;line-height: 100px;color:#32CD32" v-else>
          <img src="/@/assets/images/noData.png" style="width: 150px;height:100px;" alt="">
        </div>
      </template>
      <template #idCardFront="{ record }">
        <Image style="height: 100px" v-if="record.idCardFront" :src="record.idCardFront" width="100" height="100px" alt="" />
        <div style="height: 100px;text-align: center;line-height: 100px;color:#32CD32" v-else>
          <img src="/@/assets/images/noData.png" style="width: 150px;height:100px;" alt="">
        </div>
      </template>
      <template #idCardBack="{ record }">
        <Image style="height: 100px" v-if="record.idCardBack" :src="record.idCardBack" width="100" height="100px" alt="" />
        <div style="height: 100px;text-align: center;line-height: 100px;color:#32CD32" v-else>
          <img src="/@/assets/images/noData.png" style="width: 150px;height:100px;" alt="">
        </div>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleAgree(record)" v-auth="'imageverification:userAuthentication:edit'" type="text">
          <span style="font-size: 14px; color: #52c41a;">{{ t('通过') }}</span>
        </a-button>
        <a-button @click="handleReject(record)" v-auth="'imageverification:userAuthentication:edit'" type="text">
          <span style="font-size: 14px; color: #CD950C;">{{ t('拒绝') }}</span>
        </a-button>
        <Popconfirm
          title="是否确认删除"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
          @cancel="handleCancel"
        >
          <a-button v-auth="'imageverification:userAuthentication:edit'" type="text" style="color: red;">
            <span style="font-size: 14px;">{{ t('删除') }}</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup name="ViewsImageverificationUserAuthenticationList">
import { unref } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
import { Icon } from '/@/components/Icon';
import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
import { userAuthenticationDelete, userAuthenticationListData, userAuthenticationSave } from '/@/api/userAuthentication.ts/userAuthentication';
import { useDrawer } from '/@/components/Drawer';
import { FormProps } from '/@/components/Form';
import { Image } from 'ant-design-vue';
import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件

const { t } = useI18n('userAuthentication');
const { showMessage } = useMessage();
const { meta } = unref(router.currentRoute);

const getTitle = {
  icon: meta.icon || 'i-ant-design:book-outlined',
  value: meta.title || t('信息认证'),
};

const searchForm: FormProps = {
  baseColProps: { md: 8, lg: 6 },
  labelWidth: 90,
  schemas: [
    {
      label: t(''),
      field: 'username',
      component: 'Input',
      componentProps: {
        placeholder: t('请输入用户名'),
      }
    },
    {
      label: t(''),
      field:'status',
      component: 'Select',
      componentProps: {
        placeholder: t('请选择认证状态'),
        allowClear: true,
        options: [
          { label: t('已认证'), value: '1' },
          { label: t('未认证'), value: '0' },
          { label: t('认证失败'), value: '2' },
        ],
        onChange: handleSuccess,
      },
    },
    {
      label: t(''),
      field: 'enterpriseName',
      component: 'Input',
      componentProps: {
        placeholder: t('请输入企业名称'),
      }
    },
  ],
};

const tableColumns: BasicColumn[] = [
  {
    title: t('用户名'),
    dataIndex: 'username',
    key: 'a.username',
    sorter: true,
    width: 130,
    align: 'left',
    slot: 'firstColumn',
  },
  {
    title: t('企业名称'),
    dataIndex: 'enterpriseName',
    key: 'a.enterprise_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('用户编码'),
    dataIndex: 'userCode',
    key: 'a.user_code',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('人像信息照片'),
    dataIndex: 'facePhoto',
    key: 'a.face_photo',
    sorter: true,
    width: 130,
    align: 'left',
    slots: { customRender: 'facePhoto' },
  },
  {
    title: t('身份证人像面照片'),
    dataIndex: 'idCardFront',
    key: 'a.id_card_front',
    sorter: true,
    width: 130,
    align: 'left',
    slots: { customRender: 'idCardFront' },
  },
  {
    title: t('身份证国徽面照片'),
    dataIndex: 'idCardBack',
    key: 'a.id_card_back',
    sorter: true,
    width: 130,
    align: 'left',
    slots: { customRender: 'idCardBack' },
  },
  {
    title: t('认证状态'),
    dataIndex:'status',
    key: 'a.status',
    sorter: true,
    width: 130,
    align: 'center',
    customRender: ({ record }) => {
      const statusMap = {
        '0': t('未认证'),
        '1': t('已认证'),
        '2': t('认证失败')
      };
      return statusMap[record.status] || t('未知状态');
    }
  },
  {
    title: t('操作'),
    dataIndex: 'action',
    key: 'action',
    width: 160,
    fixed: 'right',
    slot: 'action'
  }
];

const [registerTable, { reload }] = useTable({
  api: userAuthenticationListData,
  beforeFetch: (params) => {
    return params;
  },
  columns: tableColumns,
  formConfig: searchForm,
  // 移除原 actionColumn
  // actionColumn: actionColumn,
  showTableSetting: true,
  useSearchForm: true,
  canResize: true,
});

const [registerDrawer, { openDrawer }] = useDrawer();

async function handleAgree(record: Recordable) {
  const newRecord = { ...record, status: '1' };
  try {
    const res = await userAuthenticationSave({}, newRecord);
    showMessage(res.message);
    handleSuccess(record);
  } catch (error) {
    console.error('保存失败:', error);
    showMessage('保存失败，请稍后重试');
  }
}

async function handleReject(record: Recordable) {
  const newRecord = { ...record, status: '2' };
  try {
    const res = await userAuthenticationSave({}, newRecord);
    showMessage(res.message);
    handleSuccess(record);
  } catch (error) {
    console.error('保存失败:', error);
    showMessage('保存失败，请稍后重试');
  }
}

async function handleDelete(record: Recordable) {
  const params = { tid: record.tid };
  const res = await userAuthenticationDelete(params);
  showMessage(res.message);
  handleSuccess(record);
}

function handleSuccess(record: Recordable) {
  reload({ record });
}

function handleCancel() {
  console.log('取消删除操作');
}
</script>